package com.jiayuan.cn.energy.controller;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.*;


import cn.hutool.json.JSONObject;
import com.jiayuan.cn.energy.config.AppEnum;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.servlet.view.RedirectView;
import com.jiayuan.cn.energy.util.RSAUtil;
import com.jiayuan.cn.energy.service.UserService;

// @Controller 可以到达template下面的页面
@Controller
@Slf4j
public class WebAction {

    @Resource
    UserService userService;

    @GetMapping("/")
    public String index(Principal principal, Model model) {
        log.info("home principal:{};model:{}", principal, model);
        model.addAttribute(AppEnum.NOW_TIME.val, LocalDateTime.now().format(AppEnum.TIME_FORMATTER));
        log.info("model:{}", model);
        return "index";
    }

//    @GetMapping("/login")
//    public String login(Model model) {
//        log.info("login model:{}", model);
//        model.addAttribute(AppEnum.NOW_TIME.val, LocalDateTime.now().format(AppEnum.TIME_FORMATTER));
//        model.addAttribute(AppEnum.PUBLIC_KEY.val, userService.getPublicKey());
//        log.info("model:{}", model);
//        return "/login.html";
//    }

    @GetMapping("/hi")
    public RedirectView hi(Principal principal, Model model) {
        log.info("hi principal:{};model:{}", principal, model);
        RedirectView redirectView = new RedirectView("/");
        redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
        return redirectView;
    }


    private static Set<ScopeWithDescription> withDescription(Set<String> scopes) {
        Set<ScopeWithDescription> scopeWithDescriptions = new LinkedHashSet<>();
        for (String scope : scopes) {
            scopeWithDescriptions.add(new ScopeWithDescription(scope));

        }
        return scopeWithDescriptions;
    }

    public static class ScopeWithDescription {
        private static final String DEFAULT_DESCRIPTION = "定制的权限";
        private static final Map<String, String> scopeDescriptions = new HashMap<>();

        static {
            scopeDescriptions.put(
                    "profile",
                    "验证您的身份");
            scopeDescriptions.put(
                    "message.read",
                    "了解您可以访问哪些权限");
            scopeDescriptions.put(
                    "message.write",
                    "代表您行事");
        }

        public final String scope;
        public final String description;

        ScopeWithDescription(String scope) {
            this.scope = scope;
            this.description = scopeDescriptions.getOrDefault(scope, DEFAULT_DESCRIPTION + ":" + scope);
        }
    }
}